package com.mc.developmentkit.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import com.qamaster.android.R;
import com.mc.developmentkit.bean.GetPaidInfo;
import com.mc.developmentkit.callback.AliPayListener;
import com.mc.developmentkit.config.MCConstant;
import com.mc.developmentkit.pay.MCThirdPartyPay;
import com.mc.developmentkit.utils.MCUtils;
import com.mc.developmentkit.utils.ToastUtil;
import com.mc.developmentkit.views.TitleManger;
import com.mc.developmentkit.views.VHRadioGroup;

/**
 * Created by LeBron on 2017/1/3.
 */

public class RechargeActivity extends Activity implements View.OnClickListener,VHRadioGroup.OnCheckedChangeListener{
    private TextView tv_cz_Account,tc_cz_ye,tv_hb;
    private EditText tv_cz_Money;
    private VHRadioGroup radioGroup_CZMoney;
    private RadioButton radioButton_WX, radioButton_ZFB;
    private Button btn_CZ;
    public String shangpinName=null;
    public String shangpinDel=null;
    private String shangpinJai;
    private String pingtaibi;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);
        TitleManger insetance = TitleManger.getInsetance();
        insetance.setContext(this);
        insetance.setName("充值中心");
        initView();
        initData();
    }

    private void initView() {
        tv_cz_Account = (TextView) findViewById(R.id.cz_zh);
        tv_cz_Money = (EditText) findViewById(R.id.tv_czje);
        tc_cz_ye= (TextView) findViewById(R.id.cz_ye);
        tv_hb= (TextView) findViewById(R.id.hb);
        radioGroup_CZMoney = (VHRadioGroup) findViewById(R.id.rg_bnnum);
        radioButton_WX = (RadioButton) findViewById(R.id.radio_button_wx);
        radioButton_ZFB = (RadioButton) findViewById(R.id.radio_button_zfb);
        btn_CZ = (Button) findViewById(R.id.btn_cz);

        radioGroup_CZMoney.setOnCheckedChangeListener(this);
        btn_CZ.setOnClickListener(this);

        tv_cz_Money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                radioGroup_CZMoney.clearCheck();
            }

            @Override
            public void afterTextChanged(Editable s) {
                radioGroup_CZMoney.clearCheck();
                try {
                    if(!s.toString().equals("")){
                        Double i = Double.parseDouble(s.toString());
                        if(i==0){
                            tv_hb.setText(s);
                        }else{
                            tv_hb.setText(i+"");
                        }
                    }else{
                        tv_hb.setText("0");
                    }
                }catch (Exception e){
                    ToastUtil.showToast("输入不合法");
                    tv_hb.setText("");
                }
            }
        });
    }
    private void initData(){
        if (TextUtils.isEmpty(MCUtils.LoginUser().nickname)){
            Log.e("充值中心用户昵称为空","----");
            return;
        }else{
            tv_cz_Account.setText(MCUtils.LoginUser().nickname);
        }
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_cz) {
            if (radioButton_WX.isChecked() == true) {
                wftPAY();
            } else if (radioButton_ZFB.isChecked() == true) {
                zfbPAY();
            }
        }
    }

    private void wftPAY() {
        getXin();
        if(shangpinName==null||shangpinDel==null||shangpinJai==null||MCUtils.LoginUser().id==0){
            ToastUtil.showToast("支付失败 代码：154");
            return;
        }
        GetPaidInfo getPaidInfo=new GetPaidInfo();
        getPaidInfo.good_title=shangpinName;
        getPaidInfo.body=shangpinDel;
        getPaidInfo.good_price=shangpinJai;
        getPaidInfo.code=2+"";
        getPaidInfo.userId= MCUtils.LoginUser().id+"";
        getPaidInfo.extend= "平台币充值";
        MCThirdPartyPay.getInstance().wftPay(RechargeActivity.this, MCConstant.getUserWFTPayUrl(),getPaidInfo);
    }
    private void zfbPAY() {
        getXin();
        if(shangpinName==null||shangpinDel==null||shangpinJai==null||MCUtils.LoginUser().id==0){
            ToastUtil.showToast("支付失败 代码：154");
            return;
        }
        GetPaidInfo getPaidInfo=new GetPaidInfo();
        getPaidInfo.good_title=shangpinName;
        getPaidInfo.body=shangpinDel;
        getPaidInfo.good_price=shangpinJai;
        getPaidInfo.code=2+"";
        getPaidInfo.userId= MCUtils.LoginUser().id+"";
        getPaidInfo.extend= "平台币充值";
        MCThirdPartyPay.getInstance().aliPay(RechargeActivity.this, MCConstant.getUserAliPayUrl(),getPaidInfo,aliPayListener);
    }
    AliPayListener aliPayListener=new AliPayListener() {
        @Override
        public void zfbPayResult(String json) {
            Log.e("支付宝支付",json.toString());
        }
    };

    @Override
    public void onCheckedChanged(VHRadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_1) {
            tv_cz_Money.setText("1");
            tv_hb.setText("1");

        } else if (checkedId == R.id.rb_2) {
            tv_cz_Money.setText("10");
            tv_hb.setText("10");

        } else if (checkedId == R.id.rb_3) {
            tv_cz_Money.setText("20");
            tv_hb.setText("20");

        } else if (checkedId == R.id.rb_4) {
            tv_cz_Money.setText("100");
            tv_hb.setText("100");

        }
    }


    public void getXin(){
        shangpinJai = tv_cz_Money.getText().toString();
        pingtaibi = tv_hb.getText().toString();
        if(shangpinJai.equals("")){
            ToastUtil.showToast("请输入价格");
            return;
        }
        if(shangpinJai.equals("0")){
            ToastUtil.showToast("价格不得小于0");
            return;
        }
        shangpinDel = "充值"+ pingtaibi +"平台币";
        shangpinName = "平台币充值";
    }
}
